pass --git-dir when reading git config when it was specified explicitly
authorJoey Hess <joeyh@joeyh.name>
Mon, 26 Sep 2022 18:38:34 +0000 (14:38 -0400)
committerJoey Hess <joeyh@joeyh.name>
Mon, 26 Sep 2022 18:38:34 +0000 (14:38 -0400)
Let GIT_DIR and --git-dir override git's protection against operating in a
repository owned by another user.

This is the same behavior other git commands have.

Sponsored-by: Jarkko Kniivilä on Patreon
CHANGELOG
Git/Config.hs
Git/CurrentRepo.hs

index 177e10e28b2e5695927fd6cd72033e89fa725f47..0a4c8f300c2afc62348587916d997571d990ffb5 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -38,6 +38,8 @@ git-annex (10.20220823) UNRELEASED; urgency=medium
   * Run annex.freezecontent-command and annex.thawcontent-command
     when on a crippled filesystem.
     Thanks, Reiko Asakura
+  * Let GIT_DIR and --git-dir override git's protection against operating
+    in a repository owned by another user.
 
  -- Joey Hess <id@joeyh.name>  Mon, 29 Aug 2022 15:03:04 -0400
 
index 7bf27937784dad68dbf5254b2edd4a314058a9ae..e788a2da5509f9bcf004ed8f3d41aceb07a34d52 100644 (file)
@@ -57,12 +57,20 @@ reRead r = read' $ r
 read' :: Repo -> IO Repo
 read' repo = go repo
   where
-       go Repo { location = Local { gitdir = d } } = git_config d
-       go Repo { location = LocalUnknown d } = git_config d
+       go Repo { location = Local { gitdir = d } } = git_config True d
+       go Repo { location = LocalUnknown d } = git_config False d
        go _ = assertLocal repo $ error "internal"
-       git_config d = withCreateProcess p (git_config' p)
+       git_config isgitdir d = withCreateProcess p (git_config' p)
          where
-               params = ["config", "--null", "--list"]
+               params = 
+                       -- Passing --git-dir changes git's behavior
+                       -- when run in a repository belonging to another
+                       -- user. When a gitdir is known, pass that in order
+                       -- to get the local git config.
+                       (if isgitdir && gitDirSpecifiedExplicitly repo
+                               then ["--git-dir=."]
+                               else [])
+                       ++ ["config", "--null", "--list"]
                p = (proc "git" params)
                        { cwd = Just (fromRawFilePath d)
                        , env = gitEnv repo
index bd1ab5cd4b6dd1938ad6b3b841896cfb1736e92b..3b607d7babc616c543d9a8f8b60b0d59ce1a9e21 100644 (file)
@@ -79,11 +79,11 @@ get = do
                        { gitdir = absd
                        , worktree = Just curr
                        }
-               r <- Git.Config.read $ newFrom loc
-               let r' = r { gitDirSpecifiedExplicitly = True }
-               return $ if Git.Config.isBare r'
-                       then r' { location = (location r) { worktree = Nothing } }
-                       else r'
+               r <- Git.Config.read $ (newFrom loc)
+                       { gitDirSpecifiedExplicitly = True }
+               return $ if Git.Config.isBare r
+                       then r { location = (location r) { worktree = Nothing } }
+                       else r
        configure Nothing Nothing = giveup "Not in a git repository."
 
        addworktree w r = changelocation r $ Local